package com.fjwt.gz.mch.ctrl.agent;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.entity.AgentAppEntity;
import com.fjwt.gz.db.entity.FansSourceEntity;
import com.fjwt.gz.db.vo.AgentInfoVO;
import com.fjwt.gz.mch.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.AgentAppService;
import com.fjwt.gz.service.impl.AgentInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/***
* 机构相关
*
*/
@Slf4j
@RestController
@RequestMapping("/api/subList")
public class AgentSubListController extends CommonCtrl {

    @Autowired private AgentInfoService agentInfoService;
    @Autowired private AgentAppService agentAppService;

    /**
     * 获取机构树
     */
    @PostMapping(value = "/queryOrgTree")
    public ApiRes queryOrgTree() {
        String appId = request.getHeader("appId");
        AgentAppEntity agentAppEntity = agentAppService.lambdaQuery().eq(AgentAppEntity::getAppId, appId).one();
        if (agentAppEntity == null){
            throw new RuntimeException("当前小程序暂不支持当前地区使用");
        }
        log.info("pc端获取机构树接口，请求地址：/api/subList/queryOrgTree，请求参数：{}",  agentAppEntity.getAgentNo());
        AgentInfoVO agentTree = agentInfoService.getAgentInfos(agentAppEntity.getAgentNo());
        return ApiRes.ok(agentTree.getChildren());
    }


    /**
     * 通过pid获取下级机构列表（只查询一级）
     */
    @PostMapping(value = "/querySubAgentList")
    public ApiRes querySubAgentList() {
        String appId = request.getHeader("appId");
        AgentAppEntity agentAppEntity = agentAppService.lambdaQuery().eq(AgentAppEntity::getAppId, appId).one();
        log.info("pc端获取机构树接口，请求地址：/api/subList/querySubAgentList，请求参数：{}",  agentAppEntity.getAgentNo());
        List<AgentInfoVO> subAgentList = agentInfoService.querySubAgentList(agentAppEntity.getAgentNo());
        return ApiRes.ok(subAgentList);
    }
}
